package com.bw.zg3park2212.adpter

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.HouseEntityItem
import com.bw.base.entity.MyHouseEntity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.house.HouseViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import java.text.SimpleDateFormat

class HouseAdpter(viewmodel:HouseViewModel):BaseQuickAdapter<MyHouseEntity,BaseViewHolder>(R.layout.item_earahouse) {
    lateinit var viewmodel:HouseViewModel
    init {
        this.viewmodel=viewmodel
    }
    override fun convert(helper: BaseViewHolder, item: MyHouseEntity) {
        helper.setText(R.id.tv01,""+item.dong+"栋")
        helper.setText(R.id.hourse_area,""+item.area+"m²")
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_01)
        val imageView = helper.getView<ImageView>(R.id.more01)
        imageView.setOnClickListener {
            if(recyclerView.visibility== View.GONE){
                recyclerView.visibility=View.VISIBLE
                imageView.setImageResource(R.drawable.baseline_arrow_upward_24)
            }else{
                recyclerView.visibility=View.GONE
                imageView.setImageResource(R.drawable.baseline_arrow_downward_24)
            }
        }
        var houseRvAdpter=HouseRvAdpter()
        recyclerView.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        recyclerView.adapter=houseRvAdpter
        houseRvAdpter.addChildClickViewIds(R.id.status)

        houseRvAdpter.setOnItemChildClickListener { adapter, view, position ->
            if(view.id==R.id.status){
                if (item.list[position].rented==false){
                   viewmodel.housecz(item.list.get(position).id,SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()))
                    item.list[position].rented=true
                }else{
                    item.list[position].rented=false
                    viewmodel.housetz(item.list.get(position).id)
                }
            }
            notifyDataSetChanged()
        }
        houseRvAdpter.data.clear()
        houseRvAdpter.addData(item.list)
    }
}
class HouseRvAdpter:BaseQuickAdapter<HouseEntityItem,BaseViewHolder>(R.layout.item_house) {
    override fun convert(helper: BaseViewHolder, item: HouseEntityItem) {
        helper.setText(R.id.room,"${item.id}室")
        helper.setText(R.id.qiye,item.enterpriseName)
        helper.setText(R.id.mianji,"${item.area}m²")
        val tv = helper.getView<TextView>(R.id.status)
        if(item.rented){
            tv.setText("已出租")
            tv.setBackgroundResource(R.drawable.bg_zganyong)
            tv.setTextColor(Color.BLUE)
        }else{
            tv.setText("未出租")
            tv.setBackgroundResource(R.drawable.shape_status)
            tv.setTextColor(Color.GREEN)
        }

    }
}
